<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;
use App\User;

class UserController extends Controller
{

    //控制器中间件
   //protected  $middleware=['\App\Http\Middleware\AdminLogin::class'=>[]];

    public  function __construct()
    {

      //  $this->middleware('admin.login');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //获取商品数据
        if($request->has('name')){
            $name = $request->name;
            // dd($name);
            $users = DB::table('users')->where('name','like',"%{$name}%")->paginate(2);
        }else{
            $users = DB::table('users')->paginate(2);
            $name = '';
        }
       
       return view('admin.user_index')->with('users',$users)->with('name',$name);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.user_add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request,[
           'name'=>'required',
            'email'=>'required',
            'mobile'=>'required',
            'password'=>'required',
        ]);

        $user['name']=$request->name;
        $user['email']=$request->email;
        $user['mobile']=$request->mobile;
        $user['password']=bcrypt($request->password);
        $uid=DB::table('users')->insertGetId($user);
        //附表信息
        $img=$request->file('image');
        // dd($img);
           $ext = $img->getClientOriginalExtension(); //获取上传文件名的后缀名
           $path = public_path('upload/images/');
           $name = time().rand(10,100).'x.'.$ext;
           if($img->move($path,$name)){
               $url = '/upload/images/'.$name;
               //插附表
               DB::table('usersinfo')->insert(['sex'=>$request->sex,'uid'=>$uid,'image'=>$url]);
            }
        return redirect('admin/user');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        // $user = DB::table('users')->where('id',$id)->first();
        //$userInfo = DB::table('user_infos')->where('uid',$id)->first();
        $user = DB::table('users')
              ->leftJoin('usersinfo','usersinfo.uid','=','users.id')
              ->leftJoin('address','address.uid','=','users.id')
              // ->where('address.status',1)
              ->where('users.id',$id)
              ->first();
        return view('admin.user_show')->with('user',$user);
            //->with('userInfo',$userInfo);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $user = User::find($id);
        return view('admin.user_edit')->with('user',$user);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // echo 111;die;
        $user['name']=$request->name;
        $user['email']=$request->email;
        $user['mobile']=$request->mobile;
        // $user['password']=bcrypt($request->password);
        DB::table('users')->where('id',$id)->update($user);
        return redirect('admin/user');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }



    //删除用户
    public function delUser(Request $request)
    {
        // var_dump($request->id);
        DB::table('address')->where('uid',$request->id)->delete();
        DB::table('usersinfo')->where('uid',$request->id)->delete();
        return DB::table('users')->where('id',$request->id)->delete();
    }

    //状态改变
    public function status(Request $request)
    {
        $status = $request->status;
        // echo !$status;
      
         
        return DB::table('users')->where('id',$request->id)->update(['status' =>!$status]);
    }
}
